/**
 * 设备页面JS
 * @author 张永禄
 */

/**
 * 设备页面数据表格属性配置
 */
$('#org_dataEquipmentGrid').datagrid({ 
	url:'devicetype/getDeviceByorgidbypage',
    singleSelect:true,
    rownumbers:true,
    queryParams:{orgGuid:orange.getOrganizationData().id},
    columns:[[
        {field:'guid',title:'设备主键',hidden:true}, 
        {field:'deviceName',title:'设备名',width:'12%',halign:'center'}, 
        {field:'deviceTypeName',title:'设备类型',width:'10%',halign:'center'},
        {field:'deviceStatusName',title:'状态',width:'7%',halign:'center'},
        {field:'brand',title:'品牌',width:'10%',halign:'center'},
        {field:'indate',title:'引进时间',width:'18%',halign:'center'},
        {field:'uptime',title:'启用时间',width:'18%',halign:'center'}, 
        {field:'usedTime',title:'已用年限',width:'6%',halign:'center'},
        {field:'durableYears',title:'预计使用年限',width:'11%',halign:'center'},
        {field:'isRentName',title:'是否租用',width:'6%',halign:'center'}
    ]],
    pagination:true
});
//引进时间 时间范围控制
$(function(){
	$('#add_dataEquipmen_indate').datebox().datebox('calendar').calendar({
		validator: function(date){
			return date<=new Date();
		}
	});
});
// 添加设备信息页面
function add_dataEquipment() {
	$('#add_dataEquipment').form('clear');
	$('input:radio[name="readonly"][value="0"]').prop('checked', true);
	$('#org_add_dataEquipment').window('open');
}
//修改设备页面信息
function update_dataEquipment() {
	$('#update_dataEquipment').form('clear');
	var doctorGrid = $("#org_dataEquipmentGrid");
	var selections = doctorGrid.datagrid("getSelections");
	if(selections.length == 0){
		$.messager.alert('提示','请选择需要修改的记录！'); 
	}else if(selections.length > 1){
		$.messager.alert('提示','请勿多选！'); 
	}else{
		var data = selections[0];
		var guid = data.guid;
		$('#update_dataEquipment').form('load', "devicetype/getDeviceByid?guid=" + guid);
		$('#org_update_dataEquipment').window('open');
	}
};
// 添加设备信息页面确定按钮
function add_dataEquipment_saveOK() {
	var jsonData = orange.systemPage.getFormData('add_dataEquipment');
	jsonData['orgGuid'] = orange.getOrganizationData().id;
	jsonData['deviceTypeName'] = $('#add_dataEquipment_equipmentType').combobox('getText');
	jsonData['isRentName'] = $('#add_dataEquipment_isRent').combobox('getText');
	jsonData['deviceStatusName'] = $('#add_dataEquipment_sagetatus').combobox('getText');
	var submitFlag = $('#add_dataEquipment').form('validate'); 
	if(submitFlag == true){
		var add_dataEquipmen_uptime = $('#add_dataEquipmen_uptime').datebox('getValue');
			add_dataEquipmen_uptime = add_dataEquipmen_uptime.replace(/\-/gi, "/");
		var uptime = Date.parse(add_dataEquipmen_uptime);
		var add_dataEquipmen_indate = $('#add_dataEquipmen_indate').datebox('getValue');
			add_dataEquipmen_indate = add_dataEquipmen_indate.replace(/\-/gi, "/");
		var indate = Date.parse(add_dataEquipmen_indate);
		if (uptime < indate) {
			$.messager.alert('提示', '启用时间不能早于引进日期！');
			return;
		}
		$.ajax({
			url:'devicetype/saveDevice',
			type: 'post',
		    data: jsonData,
		    success: function(data) {
		    	if(data.success == true){
		    		$.messager.show({
		    			title:'提示',
		    			msg:'保存成功',
		    		});
		    		$('#org_dataEquipmentGrid').datagrid('reload');
			    	$('#org_add_dataEquipment').window('close');
		    	}else{
		    		$.messager.alert('提示',data.errorMsg);
		    	}
		    }
		});
	}
}
// 添加设备信息页面取消按钮
function add_dataEquipment_cancelOK() {
	$('#org_add_dataEquipment').window('close');
}
// 修改设备信息页面确定按钮
function update_dataEquipment_saveOK() {
	var jsonData = orange.systemPage.getFormData('update_dataEquipment');
	jsonData['org_guid'] = orange.getOrganizationData().id;
	jsonData['deviceTypeName'] = $('#update_dataEquipment_equipmentType').combobox('getText');
	jsonData['isRentName'] = $('#update_dataEquipment_isRent').combobox('getText');
	jsonData['deviceStatusName'] = $('#update_dataEquipment_sagetatus').combobox('getText');
	var update_dataEquipmen_uptime = $('#update_dataEquipmen_uptime').datebox('getValue');
		update_dataEquipmen_uptime = update_dataEquipmen_uptime.replace(/\-/gi, "/");
	var uptime = Date.parse(update_dataEquipmen_uptime);
	var update_dataEquipmen_indate = $('#update_dataEquipmen_indate').datebox('getValue');
		update_dataEquipmen_indate =update_dataEquipmen_indate.replace(/\-/gi, "/");
	var indate = Date.parse(update_dataEquipmen_indate);
	if (uptime < indate) {
		$.messager.alert('提示', '启用时间不能早于引进日期！');
		return;
	}
	$.ajax({
		url:'devicetype/updateCmcpDevice',
		type: 'post',
	    data: jsonData,
	    success: function(data) {
	    	if(data.success == true){
	    		$.messager.show({
	    			title:'提示',
	    			msg:'修改成功',
	    		});
	    		$('#org_dataEquipmentGrid').datagrid('reload');
		    	$('#org_update_dataEquipment').window('close');
	    	}else{
	    		$.messager.alert('提示',data.errorMsg);
	    	}
	    }
	});
}
// 修改设备信息页面取消按钮
function update_dataEquipment_cancelOK() {
	$('#org_update_dataEquipment').window('close');
}
//是否租用下拉框
var dataEquipment_isRent = ["#add_dataEquipment_isRent", "#update_dataEquipment_isRent"]; 
for(var i = 0;i < dataEquipment_isRent.length; i++) {
	$(dataEquipment_isRent[i]).combobox({    
		data:$.appDictCache.get('dict_deviceisuse'),
		valueField:'code',    
		textField:'value',
		panelHeight:'auto',
	});
}
//当前使用状况下拉框
var dataEquipment_sagetatus = ["#add_dataEquipment_sagetatus", "#update_dataEquipment_sagetatus"]; 
for(var i = 0;i < dataEquipment_sagetatus.length; i++) {
	$(dataEquipment_sagetatus[i]).combobox({    
		data:$.appDictCache.get('dict_devicestatus'),
		valueField:'code',    
	    textField:'value',
		panelHeight:'auto',
	});
}
//设备类型下拉框
var dataEquipment_equipmentType = ["#add_dataEquipment_equipmentType", "#update_dataEquipment_equipmentType"]; 
for(var i = 0;i < dataEquipment_equipmentType.length; i++) {
	$(dataEquipment_equipmentType[i]).combobox({    
		data:$.appDictCache.get('dict_devicetype'),
		valueField:'code',    
	    textField:'value',
		panelHeight:'auto',
	});
}
